package net.sk.china.platform.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sk.china.platform.constants.Constants;
import net.sk.china.platform.dao.ArticleDao;
import net.sk.china.platform.model.Article;
import net.sk.china.platform.model.Category;
import net.sk.china.platform.model.system.SystemAdmin;
import net.sk.china.platform.request.ArticleRequest;
import net.sk.china.platform.request.ArticleSearchRequest;
import net.sk.china.platform.response.ArticleResponse;
import net.sk.china.platform.service.ArticleService;
import net.sk.china.platform.service.CategoryService;
import net.sk.china.platform.service.system.SystemRoleService;
import net.sk.china.common.exception.TalkException;
import net.sk.china.common.request.PageParamRequest;
import net.sk.china.common.response.CommonPage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ArticleServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2018-2024 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2024/2/21 9:55
 * // +----------------------------------------------------------------------
 */

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleDao, Article> implements ArticleService {

    @Resource
    private ArticleDao dao;

    private final CategoryService categoryService;

    private final SystemRoleService systemRoleService;

    public ArticleServiceImpl(CategoryService categoryService, SystemRoleService systemRoleService) {
        this.categoryService = categoryService;
        this.systemRoleService = systemRoleService;
    }

    /**
     * 获取文章列表
     * @param request 请求参数
     * @param pageParamRequest 分页参数
     * @return {@code PageInfo<ArticleResponse>}
     */
    @Override
    public PageInfo<ArticleResponse> getAdminList(ArticleSearchRequest request, PageParamRequest pageParamRequest) {
        Page<Article> articlePage = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        LambdaQueryWrapper<Article> query = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(request.getCid())) {
            query.eq(Article::getCid, request.getCid());
        }
        if (!StringUtils.isBlank(request.getKeywords())) {
            query.and(i -> i.or().like(Article::getTitle, request.getKeywords())
                    .or().like(Article::getAuthor, request.getKeywords())
                    .or().like(Article::getIntro, request.getKeywords()));
        }

        SystemAdmin systemAdmin = systemRoleService.getSystemAdminInfo();
        if (!systemAdmin.getRoles().equals(String.valueOf(Constants.NUM_ONE))) { query.eq(Article::getAdminId, systemAdmin.getId()); }

        query.orderByDesc(Article::getVisit).orderByDesc(Article::getId);
        List<Article> articleList = dao.selectList(query);

        ArrayList<ArticleResponse> articleVoArrayList = new ArrayList<>();
        if (articleList.size() < 1) {
            return CommonPage.copyPageInfo(articlePage, articleVoArrayList);
        }
        for (Article article : articleList) {
            ArticleResponse articleVo = new ArticleResponse();
            BeanUtils.copyProperties(article, articleVo);
            if (!StrUtil.isBlank(article.getImage()) ) {
                articleVo.setImage(article.getImage());
            }
            Category category = categoryService.getById(article.getCid());
            articleVo.setCid(ObjectUtil.isNotNull(category) ? category.getName() : "");
            articleVoArrayList.add(articleVo);
        }
        return CommonPage.copyPageInfo(articlePage, articleVoArrayList);
    }

    /**
     * 文章新增
     * @param request 文章新增参数
     * @return {@code boolean}
     */
    @Override
    public boolean create(ArticleRequest request) {
        Article article = new Article();
        BeanUtils.copyProperties(request, article);
        article.setCid(Integer.parseInt(request.getCid()));

        return save(article);
    }

    /**
     * 文章删除
     * @param id 文章id
     * @return {@code boolean}
     */
    @Override
    public boolean deleteById(Integer id) {
        getDetail(id);

        return removeById(id);
    }

    /**
     * 文章修改
     * @param id 文章id
     * @param request 文章修改参数
     * @return {@code boolean}
     */
    @Override
    public boolean updateArticle(Integer id, ArticleRequest request) {
        Article article = getDetail(id);
        BeanUtils.copyProperties(request, article);
        article.setCid(Integer.parseInt(request.getCid()));

        return updateById(article);
    }

    /**
     * 获取文章详情
     * @param id 文章id
     * @return {@code Article}
     */
    @Override
    public Article getDetail(Integer id) {
        SystemAdmin systemAdmin = systemRoleService.getSystemAdminInfo();

        Article article;
        if (systemAdmin.getRoles().equals(String.valueOf(Constants.NUM_ONE))) {
            article = getById(id);
        } else {
            LambdaQueryWrapper<Article> query = new LambdaQueryWrapper<>();
            query.eq(Article::getId, id).eq(Article::getAdminId, systemAdmin.getId());

            article = dao.selectOne(query);
        }

        if (ObjectUtil.isNull(article)) {
            throw new TalkException("文章不存在");
        }
        return article;
    }
}
